#!/bin/sh
#
# A hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook exits with
# non-zero status if the files being committed do not conform to
# the DMTCP style.
#
# To enable this hook, do this from the root of the repo:
#
# $ ln -s ../../util/hooks/pre-commit .git/hooks/pre-commit

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=f04e29e5f295620e25db4a8fc26870733f039081
fi

# Redirect output to stderr.
exec 1>&2

# Check DMTCP style.
ADDED_OR_MODIFIED=$(git diff --cached --name-only --diff-filter=AM)
if [ -n "$ADDED_OR_MODIFIED" ]; then
    # NOTE: We choose to implement this as a conditional rather than a call to
    # `xargs` on purpose. Some implementations of `xargs` will call your script
    # even if the arguments you pass in are empty. In our case, this would
    # cause us to erroneously lint every file in the repository. Additionally,
    # many implementations do not support the `-r` flag, (which instructs
    # `xargs` to not run the script if the arguments are empty), so we also
    # cannot use that.
    if command -v python; then \
      python ./util/dmtcp-style.py $ADDED_OR_MODIFIED || exit 1; \
    elif command -v python3; then \
      python3 ./util/dmtcp-style.py $ADDED_OR_MODIFIED || exit 1; \
    else \
      exit 1; \
    fi;
fi
